import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import autoprefixer from 'autoprefixer'
import { resolve } from 'path'
import legacy from '@vitejs/plugin-legacy'

export default defineConfig({
	css: {
		postcss: {
			plugins: [
				autoprefixer({
					overrideBrowserslist: ['Chrome > 40', 'ff > 31', 'ie 11'],
				}),
			],
		},
	},
	resolve: {
		alias: [
			{
				find: '@',
				replacement: resolve(__dirname, 'src'),
			},
		],
	},

	plugins: [
		vue(),
		legacy({
			targets: ['cover 99.5%'],
		}),
	],
  // 构建配置
  build: {
    target: 'es2015', // 编译目标
    outDir: 'dist',  // 输出目录
    assetsDir: 'assets', // 静态资源目录
    sourcemap: false, // 按需开启
    minify: 'terser', // 压缩方式
    cssCodeSplit: true, // CSS代码分割
    // Rollup配置
    rollupOptions: {
      output: {
        // 代码分割策略
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return 'vendor'
          }
          if (id.includes('src/router')) {
            return 'router'
          }
        },
        // 文件命名规则
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: 'assets/[ext]/[name]-[hash][extname]'
      },
      // 外部依赖排除（如有需要）
      external: ['some-cdn-library']
    },
    // Terser压缩配置
    terserOptions: {
      compress: {
        drop_console: true,  // 移除console
        drop_debugger: true  // 移除debugger
      },
      format: {
        comments: false // 移除注释
      }
    }
  },
	optimizeDeps: {
		include: ['core-js'],
	},
	server: {
		port: 80,
		host: '0.0.0.0',
	},
})
